package com.android.clinic.app.ui.doctor;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.ui.search.FlowTagLayout;
import com.android.clinic.app.ui.search.OnInitSelectedPosition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/3/6.
 * 评价医生
 */
public class EvaluationDoctorActivity extends BaseActivity {

    private FlowTagLayout mHotFlowLay;
    String tagStr = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_evaluation_doctor;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("评价医生");
        setTitleBarLeftResource(R.drawable.icon_back);
        findViewById(R.id.layout_tag).setVisibility(View.VISIBLE);
        mHotFlowLay = (FlowTagLayout) findViewById(R.id.hot_flow_layout);
        initEvaluationTags();
    }

    /**
     * 初始化评价标签
     */
    private void initEvaluationTags() {
        List<String> dataList = new ArrayList<>();
        dataList.add("意见很有帮助");
        dataList.add("态度非常好");
        dataList.add("非常敬业");
        dataList.add("非常清楚");
        dataList.add("讲解专业认真");
        dataList.add("回复很及时");
        TagAdapter tagAdapter = new TagAdapter();
        mHotFlowLay.setAdapter(tagAdapter);
        tagAdapter.addHotKeyWord(dataList);

        mHotFlowLay.setOnTagClickListener((parent, view, position) -> {
            TagAdapter.TagHolder holder = (TagAdapter.TagHolder) view.getTag();
            holder.tvTag.setSelected(!holder.tvTag.isSelected());
            if (holder.tvTag.isSelected()) {
                tagStr = tagStr + holder.tvTag.getTag().toString() + ",";
            } else {
                tagStr = tagStr.replace(holder.tvTag.getTag().toString() + ",", "");
            }
        });
    }

    private class TagAdapter extends BaseAdapter implements OnInitSelectedPosition {

        private List<String> dataList = new ArrayList<>();

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TagAdapter.TagHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(EvaluationDoctorActivity.this)
                        .inflate(R.layout.item_evaluation_tag_layout, null);
                holder = new TagAdapter.TagHolder();
                holder.tvTag = (TextView) convertView.findViewById(R.id.tv_tag);
                convertView.setTag(holder);
            } else {
                holder = (TagAdapter.TagHolder) convertView.getTag();
            }

            holder.tvTag.setText(dataList.get(position));
            holder.tvTag.setTag(dataList.get(position));
            return convertView;
        }

        @Override
        public boolean isSelectedPosition(int position) {
            return false;
        }

        private void addHotKeyWord(List<String> data) {
            dataList.addAll(data);
            notifyDataSetChanged();
        }

        class TagHolder {
            TextView tvTag;
        }
    }

    @Override
    protected void widgetClick(View v) {
        super.widgetClick(v);
        if (v.getId() == R.id.btn_submit) {
            showToast(tagStr);
        }
    }
}
